<?php
/**
 * Created by Pako
 * Date: 2018/10/4
 * Time: 17:46
 */

namespace App\Lib\Utils;


class Category
{
    /**
     * 把数组的健名设为主键值
     * @param array $data 菜单数组
     * @param string $key 主键名
     * @return array
     */
    public function setArrayKey($data, $key = "cat_id")
    {
        $arr = [];
        if ($data) {
            foreach ($data as $k => $v) {
                $arr[$v[$key]] = $v;
            }
        }
        return $arr;
    }

    /**
     * 把数据集组成树(二维数组)
     * @param $data
     * @param int $pid
     * @param int $path
     * @param int $level
     * @param string $pid_key
     * @param string $key
     * @return mixed
     */
    public function getTree(&$data, $pid = 0, $path = 0, $level = 0, $pid_key = "pid", $key = "cat_id")
    {
        if (!isset($data['old'])) {
            $data = array('new' => [], 'old' => $data);
        }
        foreach ($data['old'] as $k => $v) {
            if ($v[$pid_key] == $pid) {
                $v['path'] = $path;
                $v['level'] = $level;
                $data['new'][] = $v;
                unset($data['odl'][$k]);

                $this->getTree($data, $v[$key], $path . '-' . $v[$key], $level + 1, $pid_key, $key);
            }
        }
        return $data['new'];
    }

    /**
     * 把数组拼装成多维数组
     * Created by Paco
     * Date: 2018/5/1
     * Time: 1:28
     * @param array $data 需要组装的数组
     * @param string $pid_key 父ID的KEY值
     * @param string $key 数组的主键
     * @return array
     */
    public function getDeepTree($data, $pid = "pid", $key = '')
    {
        if (gettype($data) != 'array') {
            $data = json_decode(json_encode($data), true);
        }
        $items = array();
        foreach ($data as $v) {
            $items[$v[$key]] = $v;
        }
        $tree = array();
        foreach ($items as $k => $item) {
            if (isset($items[$item[$pid]])) {
                $items[$item[$pid]]['children'][] = &$items[$k];
            } else {
                $tree[] = &$items[$k];
            }
        }
        return $tree;

    }

    /**
     * 根据ID判断是否有子类
     *
     * @author paco jqcool.net
     * @param $id
     * @return
     */
    public function haveChild($id, $data, $pid_key = "pid")
    {
        $flag = false;
        foreach ($data as $val) {
            if ($val[$pid_key] == $id) {
                $flag = true;
                break;
            }
        }
        return $flag;
    }


    /**
     * 获取某分类的直接子分类记录
     * @param  array $categorys 数据集
     * @param  integer $catId 分类ID
     * @return array             返回该分类的直接子类数组
     */
    public function getSons($categorys, $catId = 0, $level = 0, $pid_key = "pid", $key = "id")
    {
        $sons = [];
        foreach ($categorys as $k => $item) {
            if ($item[$pid_key] == $catId) {
                $item['level'] = $level;
                $sons[] = $item;
                unset($categorys[$k]); //注销当前节点数据，降低已没用的遍历
                $sons = array_merge($sons, $this->getSons($categorys, $item[$key], $level++));
            }
        }
        return $sons;
    }

    /**
     * 获取某个分类的所有子分类ID
     * @param  array $categorys 分类数据集
     * @param  integer $catId 分类ID
     * @param  integer $self 是否包含传时来的ID
     * @return array             返回该分类下所有子类ID数组
     */
    public function getSonIds($categorys, $catId = 0, $self = 1, $pid_key = "pid", $key = "id")
    {
        $ids = [];
        if ($self) {
            $ids[] = $catId;
        }
        foreach ($categorys as $k => $item) {
            if ($item[$pid_key] == $catId) {
                $ids[] = $item[$key];
                unset($categorys[$k]); //注销当前节点数据，降低已没用的遍历
                $ids = array_merge($ids, $this->getSonIds($categorys, $item[$key], 0));
            }
        }
        return $ids;
    }

    /**
     * 获取父ID
     *
     * @param $categorys
     * @param int $catId
     * @param string $pid_key
     * @param string $key
     * @return array
     */
    public function getParentsIds($categorys, $catId = 0)
    {
        $ids = [];
        if (isset($categorys[$catId])) {
            $path = $categorys[$catId]['path'];
            $ids = explode("-", $path);
            unset($ids[0]);
        }
        return $ids;
    }
}